import React from "react";

export default function Demo(props) {
  const [count, setCount] = React.useState(0);

  React.useEffect(() => {
    let timer = setInterval(() => {
      setCount((count) => count + 1);
    }, 1000);
    return () => {
      clearInterval(timer);
    };
  }, []);

  const myRef = React.useRef()

  const add = () => {
    // setCount(count + 1)
    setCount((count) => count + 1);
  };

  // 卸载组件的回调
  const unmount = () => {
    props.root.unmount();
  };

  const show = () => {
    console.log(myRef.current.value);
  }

  return (
    <div>
      <input type="text" ref={myRef} />
      <h1>当前求和为: {count}</h1>
      <button onClick={add}>+1</button>
      <button onClick={unmount}>卸载组件</button>
      <button onClick={show}>展示数据</button>
    </div>
  );
}
